package com.imooc.common.structure.stack;

public class StackHelper {
    public static void main(String[] args) {
        //初始化栈(无元素)
        Stack stack = new Stack(new Node(), new Node());

        //栈顶和栈尾是同一指向
        stack.stackBottom = stack.stackTop;

        //指向null
        stack.stackTop.next = null;

        //进栈
        pushStack(stack, 3);
        pushStack(stack, 4);
        pushStack(stack, 5);
        traverse(stack);

    }


    /**
     * 进栈
     * @param stack
     * @param value
     */
    public static void pushStack(Stack stack,int value){
        Node newNode = new Node(value);
        newNode.next = stack.stackTop;
        stack.stackTop = newNode;
    }

    /**
     * 遍历栈 (只要栈顶指针不指向栈底指针，就一直输出)
     * @param stack
     */
    public static void traverse(Stack stack){
        Node stackTop = stack.stackTop;
        while (stackTop!=stack.stackBottom){
            System.out.println(stackTop.data);
            stackTop = stackTop.next;
        }
    }

    /**
     * 判断该栈是否为空
     * @param stack
     */
    public static boolean isEmpty(Stack stack){
        if (stack.stackTop==stack.stackBottom){
            System.out.println("该栈为空");
            return true;
        }else {
            System.out.println("该栈不为空");
            return false;
        }

    }

    /**
     * 出栈
     * @param stack
     */
    public static void popStack(Stack stack){
        if (!isEmpty(stack)){
            // 获取栈顶元素
            Node top = stack.stackTop;
            // 栈顶指针指向下一个节点
            stack.stackTop = top.next;

            System.out.println(top.data);
        }
    }

    /**
     * 清空栈
     * @param stack
     */
    public static void clearStack(Stack stack){

        stack.stackTop = null;
        stack.stackBottom = stack.stackTop;

    }

}
